<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>记住用户名</title>
    <style></style>
  </head>
  <body>
    <input type="text" id="username" /><input
      type="checkbox"
      id="remember"
    />记住用户名
    <script>
      var text = document.querySelector("#username");
      var rem = document.querySelector("#remember");
      if (localStorage.getItem("username")) {
        text.value = localStorage.getItem("username");
        rem.checked = true;
      }
      rem.addEventListener("change", function () {
        // console.log(1);
        if (rem.checked == true) {
          localStorage.setItem("username", text.value);
          console.log(1);
        } else localStorage.removeItem("username");
      });
    </script>
  </body>
</html>
